#include <stdio.h>

/*

stack_size:

"arch/arm/include/asm/thread_info.h"
#define THREAD_SIZE_ORDER   1
#define THREAD_SIZE     (PAGE_SIZE << THREAD_SIZE_ORDER)

"arch/arm/include/asm/page.h"
#define PAGE_SHIFT      12
#define PAGE_SIZE       (_AC(1,UL) << PAGE_SHIFT)

所以对于arm32平台，PAGE_SIZE大小为4k，THREAD_SIZE大小为8k；此时可以确定 init_task 的内核栈大小为8k。

$ ulimit -s #==> 8192

*/

#define __AC(X, Y) (X##Y)
#define _AC(X, Y) __AC(X, Y)

#define MY_PAGE_SHIFT 12
#define MY_PAGE_SIZE (_AC(1, UL) << MY_PAGE_SHIFT)

#define MY_THREAD_SIZE_ORDER 1
#define MY_THREAD_SIZE (MY_PAGE_SIZE << MY_THREAD_SIZE_ORDER)

int main()
{
    printf("MY_PAGE_SIZE = %lu, MY_THREAD_SIZE = %lu\n", MY_PAGE_SIZE, MY_THREAD_SIZE);
    // MY_PAGE_SIZE = 4096, MY_THREAD_SIZE = 8192,
#if 0
    char buf8[1024*  MY_THREAD_SIZE] = {}; // ==> Segmentation fault
#elif 0
    char buf4[1021 * MY_THREAD_SIZE] = {}; // ==> 没有出现 Segmentation fault,
#elif 1
    char buf1[MY_THREAD_SIZE] = {'a'};
    char buf2[2 * MY_THREAD_SIZE] = {'b'};
    char buf3[512 * MY_THREAD_SIZE] = {}; // ==> 没有出现 Segmentation fault,
    printf("buf1[0] = %c, buf1 = %s\n", buf1[0], buf1);
    buf1[MY_THREAD_SIZE - 2] = 'm';
    printf("buf1[MY_THREAD_SIZE -2] = %c\n", buf1[MY_THREAD_SIZE - 2]);
    buf2[2 * MY_THREAD_SIZE - 2] = 'n';
    printf("buf2[2* MY_THREAD_SIZE -2] = %c\n", buf2[2 * MY_THREAD_SIZE - 2]);
#endif
    printf("Hello world\n");
    return 0;
}
